Updating a Database Using Flash MX Professional 2004
Table of Contents
A Sample Update Packet Parser
Now that you have some information on the update packet and result packet formats, it's time to look at an example parser in detail. The following code snippets are from the provided ColdFusion component.
<cffunction name="saveSQLData" access="remote" returntype="string">
<cfargument name="datasource" type="string" required="true">
<cfargument name="deltaData" type="string" required="true">
...
</cffunction>
The saveSQLData() function performs the following tasks:
-
It parses the update packet, pulling out top level node data and the operation nodes.
<!--- Get the nodes from the update packet ---> <cfset deltaXML=XmlParse(deltaData)> <cfset deltaNodes=XmlSearch(deltaXML, "update_packet/*")> <cfset nodecount=ArrayLen(deltaNodes)> <!--- Now get the values passed in the update packet ---> <cfset nullValue=deltaXML.update_packet.XmlAttributes.nullValue> ...
-
It creates a SQL statement for each operation based on the type of the operation node.
<cfloop index="deltaNum" from="1" to=#nodecount#> <cfset keyFldNodes=XmlSearch(deltaXML, "update_packet/*[#deltaNum#]/field[@key='true']")> <cfset modFldNodes=XmlSearch(deltaXML, "update_packet/*[#deltaNum#]/field[@newValue]")> ... <!--- Assemble the remainder of the SQL statement based on this node's type ---> <!--- update ---> <cfif deltaNodes[deltaNum].xmlname is "update"> ... <!--- insert ---> <cfelseif deltaNodes[deltaNum].XmlName is "insert"> ... <!--- delete ---> <cfelseif #deltaNodes[deltaNum].XmlName# is "delete"> </cfloop> -
It calls the
formatFieldValue()method to format each field value based on its data type. It also checks for and modifies null values.<cfset valList=formatFieldValue(modFldNodes[fieldNum].XmlAttributes.newValue, modFldNodes[fieldNum].XmlAttributes.type, nullValue)>
-
It calls the
getNextID()function to create a new numeric key value for a specified field on inserted records. Note that the component knows for which field to generate a new key value, because the name is passed in via the update packet. This is an example of how you can add and pass in data to control how your server function will work. Alternately, in the same position in the ActionScript code, you could modify values in the update packet if you found that necessary.<cfset autoIncValue=getNextID(datasource, tableName, autoIncFldName)>
-
It runs each SQL statement against the database.
<!--- Run the generated SQL Statement against the database and catch errors ---> <cftry> <cfquery name="updateTable" datasource=#datasource# dbtype="odbc">#PreserveSingleQuotes(sqlStmt)# </cfquery> ... </cftry> -
It checks the result of the SQL operation and saves any errors to the result packet. Since each SQL operation is done individually, the CFC can match up any errors with the corresponding operation in the result.
<cfcatch type="database"> <!--- Add a new error node to the XML result packet ---> <cfset resultPacket = "#resultPacket# <operation ... </cfcatch> -
It puts the new key value for inserted records into the result packet.
<cfif (deltaNodes[deltaNum].XmlName is "insert") and (autoIncFldName NOT EQUAL "")> <cfset resultPacket = "#resultPacket# <operation op=""update... </cfif> - It returns the result packet.
Take note that nothing in the sample ColdFusion component is hard coded to work just with the data source or the table specifically. Those values are passed in either as a parameter or in the update packet. So, this is a fairly full-featured example that will work with other tables or databases after some minor changes.